/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commands;

import java.util.logging.Level;
import java.util.logging.Logger;
import servidorped.Conversation;
import servidorped.ConversationListFactory;
import servidorped.User;
import servidorped.UserListFactory;

/**
 *
 * @author Micosil
 */
public class invite implements Command {

    @Override
    public boolean execute(String[] stringlist, User user) {
        if (user.getConversationID() == null) {
            Conversation convo = new Conversation(user);
            ConversationListFactory.getList().addConversation(convo);
            user.setConversation(convo);
        }
        if (user.getConversationID() != null) {
            for (int i = 1;i < stringlist.length;i++) {
                String oneUserName = stringlist[i];
                User userToBeInvited;
                if ((userToBeInvited = UserListFactory.getList().getUserByName(oneUserName)) != null) {
                    userToBeInvited.getUserSender().setToSendln("You have been invited to conversation " + user.getConversationID());
                    user.getUserSender().setToSend("You have invited " + userToBeInvited.getUserName());
                    ConversationListFactory.getList().invite(user, userToBeInvited);
                    Logger.getGlobal().log(Level.INFO, "User {0} invited {1} to conversation {2}.", new Object[]{user.getUserName(), oneUserName, user.getConversationID()});
                } else {
                    user.getUserSender().setToSendln("The user " + oneUserName + " is not currently connected.");
                }
            }            
        }
        return true;
    }
}
